<?php 

 
function listFiles($dir, $extension) {
	$result = array();
	if ($handle = opendir($dir)) {
	    while (false !== ($file = readdir($handle))) {
	        if ($file != "." && $file != "..") {
	        	$ext =  substr(strrchr("$dir/$file",'.'),1);
	        	if ($ext == $extension) {
	        		$result[] = "$dir/$file";
				}
	        }
	    }
	    closedir($handle);
	}
	return $result;
};

if (isset($_POST['dir'])) {
	
	if (isset($_POST['format']) && $_POST['format'] == 'json') {
		
		echo "{\"files\":[\n";
		$dir = $_POST['dir'];
		$str = "";
		
		$files = listFiles($dir);
		foreach($files as $f) {
			$str .= "\t\"$f\",\n";
		}
		$str = substr_replace($str ,"",-2);
		echo $str;
		echo "]}";
	}
	else {
		
	}
}
?>